package com.amumu.drama.common.modules.idverify.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.amumu.drama.common.config.AliyunProperties;
import com.amumu.drama.common.modules.idverify.dao.IdVerifyLogMapper;
import com.amumu.drama.common.modules.idverify.service.IdVerifyService;
import com.amumu.drama.common.modules.service.VerifyService;
import com.amumu.drama.model.IdVerifyLog;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IdVerifyServiceImpl implements IdVerifyService {

    private static final Logger LOGGER = LoggerFactory.getLogger(IdVerifyServiceImpl.class);

    @Autowired
    private VerifyService verifyService;
    @Autowired
    private IdVerifyLogMapper idVerifyLogMapper;
    @Autowired
    private AliyunProperties properties;

    @Override
    public Boolean verifyIdCard(Long memberId, String realName, String idNumber) {
        try {
            if (!properties.getEnableVerifyIdCard()) {
                return true;
            }
            String response = verifyService.verifyIdCard(realName, idNumber);
            if (Strings.isBlank(response)) {
                throw new RuntimeException("实名接口返回空");
            }
            JSONObject responseMap = JSONUtil.parseObj(response);
            if (MapUtil.isEmpty(responseMap)) {
                throw new RuntimeException("解析实名接口返回失败");
            }
            String respMessage = responseMap.getStr("respMessage");
            boolean verifyStatus = "0000".equals(responseMap.get("respCode").toString()) && "身份证信息匹配".equals(respMessage);
            if (!verifyStatus) {
                throw new RuntimeException(respMessage);
            }
            this.createIdVerifyLog(memberId, realName, idNumber, 1);
            return true;
        } catch (Exception e) {
            LOGGER.error("{}-{}-{}实名认证异常:{}", memberId, realName, idNumber, e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Integer queryMemberVerifiedStatus(Long memberId) {
        try {
            IdVerifyLog idVerifyLog = idVerifyLogMapper.selectById(memberId);
            if (idVerifyLog == null) {
                return null;
            }
            return idVerifyLog.getStatus();
        } catch (Exception e) {
            LOGGER.error("{}-queryMemberVerified error:{}", memberId, e.getMessage());
            throw e;
        }
    }

    @Override
    public Integer createIdVerifyLog(Long memberId, String realName, String idNumber, Integer status) {
        IdVerifyLog idVerifyLog = new IdVerifyLog();
        idVerifyLog.setRealName(realName);
        idVerifyLog.setIdNumber(idNumber);
        idVerifyLog.setMemberId(memberId);
        idVerifyLog.setStatus(status);
        try {
            return idVerifyLogMapper.insert(idVerifyLog);
        } catch (Exception e) {
            LOGGER.error("{}-{}createIdVerifyLog error:{}", realName, idNumber, e.getMessage());
            throw e;
        }
    }
}
